package com.ys.filelib.filedialog;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class StorageUtils {

    private static StorageManager mStorageManager;

    private static final String sLocalRootPath = Environment.getExternalStorageDirectory().getAbsolutePath();

    public static String getLocalRootPath() {
        return sLocalRootPath;
    }

    /***
     * 获取所有的存储设备
     * @return
     */
    public static List<String> getAllExternalStorage() {
        List<String> storages = new ArrayList<>();
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                List<StorageVolume> volumes = mStorageManager.getStorageVolumes();
                Field mPath = StorageVolume.class.getDeclaredField("mPath");
                mPath.setAccessible(true);
                for (StorageVolume volume : volumes) {
                    String state = volume.getState();
                    File file = (File) mPath.get(volume);
                    if (file != null && Environment.MEDIA_MOUNTED.equals(state)) {
                        storages.add(file.getAbsolutePath());
                    }
                }
            } else {
                Class<?> volumeInfo = Class.forName("android.os.storage.VolumeInfo");
                Method getVolumes = mStorageManager.getClass().getMethod("getVolumes");
                //获取所有挂载的设备（内部sd卡、外部sd卡、挂载的U盘）
                List result = (List) getVolumes.invoke(mStorageManager);
                Method getPath = volumeInfo.getMethod("getPath");
                for (int i = 0; i < result.size(); i++) {
                    Object storageVolume = result.get(i);
                    File filePath = (File) getPath.invoke(storageVolume);
                    if (filePath != null) {
                        storages.add(filePath.getAbsolutePath());
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return storages;
    }


    /**
     * 获取存储介质的标签名称
     * @param rootDir 路径
     */
    @SuppressWarnings("all")
    public static String getPathDescribe(String rootDir) {
        try {
            List<StorageVolume> volumes = mStorageManager.getStorageVolumes();
            Field mPath = StorageVolume.class.getDeclaredField("mPath");
            Field mDescription = StorageVolume.class.getDeclaredField("mDescription");
            mPath.setAccessible(true);
            mDescription.setAccessible(true);
            for(StorageVolume volume : volumes) {
                File file = (File) mPath.get(volume);
                if (rootDir.contains(file.getAbsolutePath())) {
                    return (String) mDescription.get(volume);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FileUtil.getName(rootDir);
    }

    public static boolean isLocalStorage(String path) {
        if (path == null) {
            return false;
        }
        return path.startsWith(sLocalRootPath);
    }

    public static void init(Context context) {
        mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
    }

}
